﻿using System;
using System.Threading.Tasks;
using Top.Data;
using Top.Web;
using Top.Base.Common;
using System.Diagnostics;
using Top.PointStore.Business.Models;
using System.Collections.Generic;
using Top.Web.Api;
using Top.Business.OAuth;

namespace Top.PointStore.Business
{
    public class OAuthInfoManager
    {

        public static async Task<object> CheckOAuthInfo(OAuthUser oauthuser)
        {
            //检查本地库，userToken 是否存在
            try
            {
                bool isExist = await GetUserToken(oauthuser.UserToken.ToString());
                if (!isExist)
                {
                    await AddOAuthInfo(oauthuser.UserToken, oauthuser);
                }
            }
            catch
            {
                throw new WebApiException("检测用户信息时出错");
            }
            return 1;
        }

        public static async Task<string> GetUserToken(int brokerKid, int adminKid)
        {
            using (var db = new DbTour())
            {
                if (brokerKid > 0)
                {
                    const string sql = "select F_UserToken from t_OAuthInfo   where F_BrokerKid={0}";
                    return await db.Sql(sql, brokerKid).ExecuteScalar<string>();
                }
                else
                {
                    const string sql = "select F_UserToken from t_OAuthInfo   where F_AdminKid={0}";
                    return await db.Sql(sql, adminKid).ExecuteScalar<string>();
                }
            }
        }

        public static async Task<bool> GetUserToken(string userToken)
        {
            using (var db = new DbTour())
            {
                const string sql = "select count(0) from t_OAuthInfo  where F_usertoken={0}";
                return await db.Sql(sql, userToken).ExecuteScalar<int>() > 0 ? true : false;
            }
        }

        public static async Task<bool> AddOAuthInfo(Guid userToken, OAuthUser OAuthUser = null)
        {
            if (OAuthUser == null)
            {
                OAuthUser = OAuthHelper.GetUser(userToken.ToString());
            }
            using (var db = new DbTour())
            {
                string sqlInsert = "insert T_OAuthInfo(F_UserToken,F_BrokerKid,F_AdminKid) values({0},{1},{2})";
                return await db.Sql(sqlInsert, OAuthUser.UserToken, OAuthUser.BrokerID, OAuthUser.CrmUserID).ExecuteNonQuery() > 0;
            }
        }

        public static async Task<bool> AddOAuthInfo(T_OAuthInfo oauthUser)
        {
            using (var db = new DbTour())
            {
                string sqlInsert = "insert T_OAuthInfo(F_UserToken,F_BrokerKid,F_AdminKid) values({0},{1},{2})";
                return await db.Sql(sqlInsert, oauthUser.F_UserToken, oauthUser.F_BrokerKid, oauthUser.F_AdminKid).ExecuteNonQuery() > 0;
            }
        }

        public static OAuthUser GetOAuthUserByGuid(int userKid, string accessToken, string userToken, string userType)
        {
            OAuthUser userInfo = default(OAuthUser);
            if (string.IsNullOrWhiteSpace(accessToken))
            {
                if (string.IsNullOrWhiteSpace(userToken))
                {
                    if (userKid == 0)
                    {
                        throw new WebApiException("缺少关键信息，无法找到该用户");
                    }
                    else
                    {
                        if (userType == "T_Broker")
                        {
                            userInfo = OAuthHelper.GetUserByBrokerKid(userKid);
                        }
                        else
                        {
                            userInfo = OAuthHelper.GetUserByAdminKid(userKid);
                        }
                    }
                }
                else
                {
                    userInfo = OAuthHelper.GetUser(userToken);
                }
            }
            else
            {
                userInfo = OAuthHelper.GetUserByAk(accessToken);
            }
            if (userInfo == null)
            {
                throw new WebApiException("请重新登录");
            }
            return userInfo;
        }

        public static async Task<List<string>> GetAllOAuth()
        {
            using (var db = new DbTour())
            {
                const string sql = "select F_UserToken from t_OAuthInfo ";
                return await db.Sql(sql).ToList<string>();
            }
        }
    }

    public class T_OAuthInfo
    {
        public int Kid { get; set; }

        public string F_UserToken { get; set; }

        public int F_BrokerKid { get; set; }

        public int F_AdminKid { get; set; }

        public DateTime F_AddTime { get; set; }
    }
}
